﻿#include "CallPython.h"

#include <QVBoxLayout>
#include <QDebug>
#include <Python.h>

CallPython::CallPython(QWidget *parent) : QWidget(parent)
{
    this->setWindowTitle(QStringLiteral("Python调用测试"));
    this->resize(400, 150);
    this->setAttribute(Qt::WA_DeleteOnClose);

    m_pTestBtn1 = new QPushButton(QStringLiteral("打印Hello World"), this);
    connect(m_pTestBtn1, SIGNAL(clicked(bool)), this, SLOT(test1()));

    m_pTestBtn2 = new QPushButton(QStringLiteral("使用turtle绘制小猪佩奇"), this);
    connect(m_pTestBtn2, SIGNAL(clicked(bool)), this, SLOT(test2()));

    m_pTestBtn3 = new QPushButton(QStringLiteral("使用tkinter制作登录界面"), this);
    connect(m_pTestBtn3, SIGNAL(clicked(bool)), this, SLOT(test3()));

    m_pTestBtn4 = new QPushButton(QStringLiteral("百度贴吧图片爬虫"), this);
    connect(m_pTestBtn4, SIGNAL(clicked(bool)), this, SLOT(test4()));

    QVBoxLayout *pVLayout = new QVBoxLayout;
    pVLayout->addWidget(m_pTestBtn1);
    pVLayout->addWidget(m_pTestBtn2);
    pVLayout->addWidget(m_pTestBtn3);
    pVLayout->addWidget(m_pTestBtn4);
    this->setLayout(pVLayout);
}

void CallPython::test1()
{
    //初始化python模块
    Py_Initialize();
    if (!Py_IsInitialized())
        return;

    //导入test1.py模块
    PyObject* pModule = PyImport_ImportModule("test1");
    if (!pModule)
    {
        qDebug() << QStringLiteral("打开python文件失败！");
        return;
    }

    //获取test1模块中的hello函数
    PyObject* pFunc= PyObject_GetAttrString(pModule, "hello");

    //注释掉的这部分是另一种获得test1模块中的hello函数的方法
    /*PyObject* pDict = PyModule_GetDict(pModule);
    if (!pDict)
    {
      printf("Cant find dictionary.\n");
      return -1;
    }
    PyObject* pFunc = PyDict_GetItemString(pDict, "hello");*/

    if (!pFunc)
    {
        qDebug() << QStringLiteral("获取hello函数失败！");
        return;
    }

    //调用hello函数
    PyObject_CallFunction(pFunc, NULL);

    //释放python
    Py_Finalize();
}

void CallPython::test2()
{
    //初始化python模块
    Py_Initialize();
    if (!Py_IsInitialized())
        return;

    //导入test2.py模块
    PyObject* pModule = PyImport_ImportModule("test2");
    if (!pModule)
    {
        qDebug() << QStringLiteral("打开python文件失败！");
        return;
    }

    //获取test2模块中的main函数
    PyObject* pFunc= PyObject_GetAttrString(pModule, "main");

    if (!pFunc)
    {
        qDebug() << QStringLiteral("获取main函数失败！");
        return;
    }

    //调用main函数
    PyObject_CallFunction(pFunc, NULL);

    //释放python
    Py_Finalize();
}

void CallPython::test3()
{
    //初始化python模块
    Py_Initialize();
    if (!Py_IsInitialized())
        return;

    //导入test3.py模块
    PyObject* pModule = PyImport_ImportModule("test3");
    if (!pModule)
    {
        qDebug() << QStringLiteral("打开python文件失败！");
        return;
    }

    //获取test3模块中的login函数
    PyObject* pFunc= PyObject_GetAttrString(pModule, "login");

    if (!pFunc)
    {
        qDebug() << QStringLiteral("获取login函数失败！");
        return;
    }

    //调用login函数
    PyObject_CallFunction(pFunc, NULL);

    //释放python
    Py_Finalize();
}

void CallPython::test4()
{
    //初始化python模块
    Py_Initialize();
    if (!Py_IsInitialized())
        return;

    //导入test4.py模块
    PyObject* pModule = PyImport_ImportModule("test4");
    if (!pModule)
    {
        qDebug() << QStringLiteral("打开python文件失败！");
        return;
    }

    //获取test4模块中的main函数
    PyObject* pFunc= PyObject_GetAttrString(pModule, "main");

    if (!pFunc)
    {
        qDebug() << QStringLiteral("获取main函数失败！");
        return;
    }

    //调用main函数
    PyObject_CallFunction(pFunc, NULL);

    //释放python
    Py_Finalize();
}

